{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "51CTO课程频道：http://edu.51cto.com/lecturer/index/user_id-12330098.html<br>\n",
    "优酷频道：http://i.youku.com/sdxxqbf<br>\n",
    "微信公众号：深度学习与神经网络<br>\n",
    "Github：https://github.com/Qinbf<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import numpy as np\n",
    "from six.moves import xrange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     0                                                  1  \\\n",
      "0   738845194850773558                               -5833678375673307423   \n",
      "1  3738968195649774859                                2027693463582123305   \n",
      "2  4738849194894773882                                1127459907694805235   \n",
      "3  7739004195693774975  2904932941037075699,1160326435131345730,725917...   \n",
      "4 -7261194805221226386                               -5833678375673307423   \n",
      "\n",
      "                  2     3                                                  4  \\\n",
      "0             c0,c1    w0  c0,c1,c2,c3,c4,c5,c6,c7,c0,c1,c8,c9,c10,c11,c1...   \n",
      "1           c39,c40   w24  c41,c42,c43,c39,c40,c4,c44,c45,c46,c47,c48,c49...   \n",
      "2    c172,c31,c0,c1  w102                                                NaN   \n",
      "3   c39,c40,c5,c173  w103  c39,c40,c23,c21,c174,c74,c5,c173,c17,c35,c39,c...   \n",
      "4  c36,c31,c45,c237  w148                                          c238,c239   \n",
      "\n",
      "                                                   5  \n",
      "0  w0,w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,...  \n",
      "1  w24,w25,w26,w27,w28,w6,w29,w30,w11,w31,w32,w33...  \n",
      "2                                                NaN  \n",
      "3  w104,w105,w11,w21,w24,w6,w106,w23,w54,w24,w107...  \n",
      "4                                          w149,w150  \n"
     ]
    }
   ],
   "source": [
    "topic_info = pd.read_table(\"./ieee_zhihu_cup/topic_info.txt\",sep='\\t',header=None)\n",
    "print(topic_info.iloc[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 话题字典\n",
    "topic_dict = {}\n",
    "for i in xrange(topic_info.shape[0]):\n",
    "    topic_dict[i] = topic_info.iloc[i][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predict = open('predict.txt', \"r\")\n",
    "examples = predict.readlines()\n",
    "text = np.array([line.split(\" \") for line in examples])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 217360/217360 [00:01<00:00, 160389.86it/s]\n"
     ]
    }
   ],
   "source": [
    "label = []\n",
    "for line in tqdm(text):\n",
    "    num2label = []\n",
    "    for i in xrange(5):\n",
    "        num2label.append(topic_dict[int(line[i])]) # 把0-1999编号转成原来的id\n",
    "    label.append(num2label)\n",
    "label = np.array(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.savetxt(\"temp.txt\",label,fmt='%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def clean_str(string):\n",
    "    string = re.sub(r\" \", \",\", string)\n",
    "    return string\n",
    "\n",
    "file1 = open('temp.txt', \"r\")\n",
    "examples = file1.readlines()\n",
    "examples = [clean_str(line) for line in examples]\n",
    "file1.close()\n",
    "\n",
    "file1 = open('temp.txt', \"w\")\n",
    "file1.writelines(examples)\n",
    "file1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                                   0\n",
      "0  -3517637179126242000,-4653836020042332281,4715...\n",
      "1  3418451812342379591,2858911571784840089,238291...\n",
      "2  -7358589937244777363,-5265476641576484497,7477...\n",
      "3  -7046289575185911002,-4653836020042332281,-587...\n",
      "4  4715442001886462944,-8963554618409314978,11274...\n"
     ]
    }
   ],
   "source": [
    "# predict文件导入\n",
    "predict_file = 'temp.txt'\n",
    "predict_reader = pd.read_table(predict_file,sep=' ',header=None)\n",
    "print(predict_reader.iloc[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     0                                                  1  \\\n",
      "0  6215603645409872328  c924,c531,c102,c284,c188,c104,c98,c107,c11,c11...   \n",
      "1  6649324930261961840  c346,c1549,c413,c294,c675,c504,c183,c74,c541,c...   \n",
      "2 -4251899610700378615  c96,c97,c97,c98,c99,c100,c101,c141,c42,c42,c10...   \n",
      "\n",
      "                                                   2  \\\n",
      "0  w1340,w1341,w55,w1344,w58,w6,w24178,w26959,w47...   \n",
      "1  w40132,w1357,w1556,w1380,w2464,w33,w16791,w109...   \n",
      "2  w53,w54,w1779,w54,w1309,w54,w369,w949,w65587,w...   \n",
      "\n",
      "                                                   3  \\\n",
      "0  c1128,c529,c636,c572,c1321,c139,c540,c223,c510...   \n",
      "1                                                NaN   \n",
      "2  c149,c148,c148,c42,c185,c95,c95,c186,c186,c186...   \n",
      "\n",
      "                                                   4  \n",
      "0  w4094,w1618,w20104,w19234,w1097,w1005,w4228,w2...  \n",
      "1                                                NaN  \n",
      "2                                                NaN  \n"
     ]
    }
   ],
   "source": [
    "# 导入question_train_set\n",
    "eval_reader = pd.read_table('./ieee_zhihu_cup/question_eval_set.txt',sep='\\t',header=None)\n",
    "print(eval_reader.iloc[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     0                                                  0\n",
      "0  6215603645409872328  -3517637179126242000,-4653836020042332281,4715...\n",
      "1  6649324930261961840  3418451812342379591,2858911571784840089,238291...\n",
      "2 -4251899610700378615  -7358589937244777363,-5265476641576484497,7477...\n",
      "3  6213817087034420233  -7046289575185911002,-4653836020042332281,-587...\n",
      "4 -8930652370334418373  4715442001886462944,-8963554618409314978,11274...\n"
     ]
    }
   ],
   "source": [
    "final_predict = pd.concat([eval_reader.ix[:,0],predict_reader],axis=1)\n",
    "print(final_predict.iloc[0:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "final_predict.to_csv('temp.txt', header=None, index=None, sep=',')\n",
    "\n",
    "final_file = open('temp.txt', \"r\")\n",
    "final_examples = final_file.readlines()\n",
    "final_examples = [re.sub(r'\"',\"\",line) for line in final_examples]\n",
    "final_file.close()\n",
    "\n",
    "final_file = open('final_predict.csv', \"w\")\n",
    "final_file.writelines(final_examples)\n",
    "final_file.close()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
